package controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;

import service.EmployeeService;
import dao.AssociationDao;
import dao.EmployeeDao;
import dao.PayMethodDao;
import dao.PaymentReportDao;
import entity.Association;
import entity.Commissioned;
import entity.Contractor;
import entity.Employee;
import entity.PayMethod;
import entity.PaymentReport;
import entity.Salaried;
import factory.EmployeeServiceFactory;

@Stateless
public class EmployeeController {
	@Inject
	EmployeeDao employeeDao;
	@Inject
	AssociationDao associationDao;
	@Inject
	PaymentReportDao paymentReportDao;
	@Inject
	EmployeeServiceFactory factory;
	@Inject 
	PayMethodDao paymethodDao;
	public void registerEmployee(Employee employee) {
		employeeDao.createEmployee(employee);
		if (employee.getAssociation() != null) {
			employee.getAssociation().setEmployee(employee);
			associationDao.registerAssociation(employee.getAssociation());
		}
		if(employee.getPayMethod()!=null){
			employee.getPayMethod().setEmployee(employee);
			paymethodDao.registerPayMethod(employee.getPayMethod());
		}
	}

	public int deleteEmployee(Employee employee) {
		return employeeDao.deleteEmployee(employee);
	}

	public void updateEmployee(Employee employee) {
		if (employee.getAssociation() != null) {
			if(associationDao.findAssociation(employee.getAssociation().getId())!=null){
				associationDao.updateAssociation(employee.getAssociation());
			}else{
				employee.getAssociation().setEmployee(employee);
				associationDao.registerAssociation(employee.getAssociation());
			}
			
		}
		if(employee.getPayMethod()!=null){
			employee.getPayMethod().setEmployee(employee);
			if(paymethodDao.findPayMethod(employee.getPayMethod().getId())!=null){
				paymethodDao.updatePayMethod(employee.getPayMethod());
			}else{
				paymethodDao.registerPayMethod(employee.getPayMethod());
			}
			
		}
		employeeDao.updateEmployee(employee);
	}

	public List<PaymentReport> PaymentReports(Employee employee){
		return paymentReportDao.EmployeePaymentReports(employee);
	}
	
	public Employee CredentialControl(String Username, String Password) {
		List<Employee> AllEmployee = employeeDao.findAllEmployee();
		Employee employee=null;
		Employee selected;
		for (int i = 0; i < AllEmployee.size(); i++) {
			selected = AllEmployee.get(i);
			if (selected.getUsername().equals(Username)
					&& selected.getPassword().equals(Password)) {
				employee = selected;
				
			}
			
		}
		return employee;
	}

	public List<Employee> ListOfAllEmployees() {
		return employeeDao.findAllEmployee();
	}

	public String TypeEmployee(Employee employee) {
		if (employee instanceof Commissioned) {
			return "Commissioned";
		} else if (employee instanceof Contractor) {
			return "Contractor";
		} else if (employee instanceof Salaried) {
			return "Salaried";
		}
		return "No type";
	}

	public String registerAssociation(Association association, Employee employee) {
		association.setEmployee(employee);
		return associationDao.registerAssociation(association);
	}

	public Employee findEmployee(int getID) {
		return employeeDao.findEmployee(getID);
	}

	public List<PaymentReport> RunPayRollMethod() {

		List<PaymentReport> PayReport = new ArrayList<PaymentReport>();
		List<Employee> list = employeeDao.findAllEmployee();
		EmployeeService service;
		PaymentReport pay;
		
		//System.out.println(format.format(todayDate));
		for (int i = 0; i < list.size(); i++) {
			service = factory.createEmployeeService(list.get(i));
			Date lastPay=paymentReportDao.lastPayDay(list.get(i));
			if (service.getPaymentCheck().checkPay()) {
					pay = new PaymentReport();
					pay.setAmount(service.CalcAbsolutePay());
					pay.setEmployee(list.get(i));
					PayReport.add(pay);
				
			}
		}
		return PayReport;
	}
	public void PayEmployee(PaymentReport report){
		paymentReportDao.createPaymentReport(report);
	}
	public boolean checkCodFisc(Employee employee){
		List<Employee> listAllEmployees=ListOfAllEmployees();
		System.out.println("entra");
		for(int i=0;i<listAllEmployees.size();i++){
			System.out.println(listAllEmployees.get(i).getCodfisc());
			if(listAllEmployees.get(i).getCodfisc().compareTo(employee.getCodfisc())==0){
				System.out.println("trovato"+listAllEmployees.get(i).getCodfisc());
				return false;
				}
		}
		return true;
	}
	public boolean checkUsername(Employee employee){
		List<Employee> listAllEmployees=ListOfAllEmployees();
		for(int i=0;i<listAllEmployees.size();i++){
			if(listAllEmployees.get(i).getUsername().compareTo(employee.getUsername())==0)
				return false;
		}
		return true;
	}
	public int deleteAssociation(Employee employee){
		return associationDao.deleteAssociation(employee.getAssociation());
	}
	public int deletePayMethod(Employee employee){
		return paymethodDao.deletePaymethod(employee.getPayMethod());
	}
}
